// [1007]行相等的最少多米诺旋转
struct Solution;

//leetcode submit region begin(Prohibit modification and deletion)
impl Solution {
    pub fn min_domino_rotations(tops: Vec<i32>, bottoms: Vec<i32>) -> i32 {
        let min_rot = |target| {
            let mut to_top = 0;
            let mut to_bottom = 0;
            for (&x, &y) in tops.iter().zip(bottoms.iter()) {
                if x != target && y != target {
                    return i32::MAX;
                } else if x != target {
                    to_top += 1;
                } else if y != target {
                    to_bottom += 1;
                }
            }
            to_top.min(to_bottom)
        };
        let ans = min_rot(tops[0]).min(min_rot(bottoms[0]));
        if ans == i32::MAX {
            -1
        } else {
            ans
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)

#[test]
fn main() {
    let tops = vec![2, 1, 2, 4, 2, 2];
    let bottoms = vec![5, 2, 6, 2, 3, 2];
    assert_eq!(Solution::min_domino_rotations(tops, bottoms), 2);
    let tops = vec![3, 5, 1, 2, 3];
    let bottoms = vec![3, 6, 3, 3, 4];
    assert_eq!(Solution::min_domino_rotations(tops, bottoms), -1);
}
